<template>
  <div class="login" v-loading.body="isloding">
    <el-form ref="form" :model="form" class="formdata" :rules="rules">
      <img src="../../assets/common/login-logo.png" alt="" class="thisimg" />
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          prefix-icon="el-icon-lock"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { dianhuahaoma, mimachangdu } from '../../utils/validate'
export default {
  name: 'login',
  data () {
    return {
      // 控制遮罩层显示隐藏
      isloding: false,
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: dianhuahaoma,
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          // { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
          { validator: mimachangdu, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.form.validate(async result => {
        try {
          this.isloding = true
          await this.$store.dispatch('user/login', {
            mobile: this.form.mobile,
            password: this.form.password
          })
          this.isloding = true
          this.$router.push('/dashboard')
        } catch {
          console.log('登陆失败')
        } finally {
          this.isloding = false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background-image: url('~@/assets/common/login.jpg');
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  .formdata {
    width: 450px;
  }
  .btn {
    width: 450px;
    background-color: #407ffe;
    color: #fff;
    border: 0;
  }
  .thisimg {
    width: 100%;
  }
}
</style>
